package com.zero2oneit.mall.search.service.impl;

import com.zero2oneit.mall.common.bean.goods.GoodsSku;
import com.zero2oneit.mall.common.utils.EsIndexConstant;
import com.zero2oneit.mall.common.utils.R;
import com.zero2oneit.mall.common.utils.bootstrap.BoostrapDataGrid;
import com.zero2oneit.mall.search.feign.goods.BusinessFeign;
import com.zero2oneit.mall.search.mapper.EsGoodProMapper;
import com.zero2oneit.mall.search.service.TcGoodEsService;
import com.zero2oneit.mall.search.vo.YzProSkuVo;
import com.zero2oneit.mall.search.vo.YzProVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * Description:
 *
 * @author yjj
 * @date 2020/12/15 10:05
 */
@Service
public class TcGoodEsServiceImpl implements TcGoodEsService {

    @Autowired
    private RestHighLevelClient esClient;

    @Autowired
    private EsGoodProMapper esMapper;

    @Autowired
    private BusinessFeign businessFeign;

    @Override
    public Map tcShopGood(String id) {
        R r = businessFeign.businessTcCategoryAll(id);
        List<Map> typeList= (List<Map>) r.getData();
        Map map = esMapper.tcSearch(id);
        List<SearchHit> proList = Arrays.asList((SearchHit[]) map.get("proHit"));
        List<SearchHit> skuList = Arrays.asList((SearchHit[]) map.get("skuHit"));
        List<YzProVo>  resultList=new ArrayList<>(typeList.size());
        typeList.forEach(typeMap->{
            YzProVo proVo=new YzProVo();
            proVo.setName((String) typeMap.get("name"));
            proList.forEach(proHit->{
                Map<String, Object> proMap = proHit.getSourceAsMap();
                if (String.valueOf(typeMap.get("id")).equals(String.valueOf(proMap.get("bussTypeId")))){
                    YzProSkuVo yzProSkuVo = new YzProSkuVo();
                    yzProSkuVo.setMap(proMap);
                    skuList.forEach(skuHit->{
                        Map<String, Object> skuMap = skuHit.getSourceAsMap();
                        if (skuMap.get("productId").equals(proMap.get("productId"))){
                            yzProSkuVo.getSkuList().add(skuMap);
                        }
                    });
                    proVo.getList().add(yzProSkuVo);
                }
            });
            resultList.add(proVo);
        });
        Map<String,Object> resultMap=new HashMap<>(2);
        resultMap.put("typeList",typeList);
        resultMap.put("list",resultList);
        return resultMap;
    }

    @Override
    public List productSkuByProId(String id) {
        return esMapper.esSelectList(EsIndexConstant.SKU_FNTC_INDEX, termQuery("productId", id), GoodsSku.class,null);
    }

    @Override
    public BoostrapDataGrid tcProSearch(Map<String, String> map) {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(termQuery("businessId",map.get("businessId")))
                .must(matchPhraseQuery("productName",map.get("keyword")));
        BoostrapDataGrid boostrapDataGrid = esMapper.esSelectCommon(EsIndexConstant.GOOD_FNTC_INDEX, queryBuilder, Map.class,
                null, null, null,1);
        List<Map> rows = (List<Map>) boostrapDataGrid.getRows();
        if (rows.size()!=0){
            Map skuMap = esMapper.yzSkuList(rows, EsIndexConstant.SKU_FNTC_INDEX);
            List<YzProSkuVo> skuVoList =new ArrayList<>();
            for (int i = 0; i < rows.size(); i++) {
                List<SearchHit> hitList = Arrays.asList((SearchHit[])skuMap.get(rows.get(i)));
                List<Map> list=new ArrayList<>();
                for (int j = 0; j < hitList.size(); j++) {
                    Map sourceAsMap = hitList.get(j).getSourceAsMap();
                    list.add(sourceAsMap);
                }
                skuVoList.add(new YzProSkuVo(rows.get(i),list));
            }
            return  new BoostrapDataGrid(boostrapDataGrid.getTotal(), boostrapDataGrid.getTotal() ==0 ? Collections.EMPTY_LIST :skuVoList);
        }
        return boostrapDataGrid;
    }
}
